﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _0912089
{
    public class CheckBox : SingleView
    {
        private Texture2D _checkTexture;
        private Texture2D _uncheckTexture;
        private bool _isChecked;

        private bool isLeftMouseClicked = false;

        public bool IsChecked
        {
            get { return _isChecked; }
            set { _isChecked = value; }
        }

        public CheckBox(string strCheckTexture,string strUnCheckTexture, bool ischecked, Vector2 pos, float scale)
        {
            _checkTexture = GameBase.Constant.content.Load<Texture2D>(strCheckTexture);
            _uncheckTexture = GameBase.Constant.content.Load<Texture2D>(strUnCheckTexture);

            _isChecked = ischecked;

            Position = pos;
            _scale = scale;
            _size = new Vector2(_checkTexture.Width, _checkTexture.Height);
        }

        public override void draw(GameTime gameTime, SpriteBatch spriteBatch, Color color)
        {
            if (_isChecked)
                spriteBatch.Draw(_checkTexture, Position, null, color, 0, Vector2.Zero, Scale, SpriteEffects.None, 0);
            else
            {
                spriteBatch.Draw(_uncheckTexture, Position, null, color, 0, Vector2.Zero, Scale, SpriteEffects.None, 0);
            }
        }

        public bool isContainPoint(Vector2 point)
        {
            if (point.X >= Position.X && point.X <= Position.X + Size.X * Scale &&
                point.Y >= Position.Y && point.Y <= Position.Y + Size.Y * Scale)
                return true;
            return false;
        }

        public override void update(GameTime gameTime)
        {
            base.update(gameTime);

            MouseState ms = Mouse.GetState();
            if (ms.LeftButton == ButtonState.Pressed)
                isLeftMouseClicked = true;
            if (ms.LeftButton == ButtonState.Released && isLeftMouseClicked)
            {
                isLeftMouseClicked = false;
                if (isContainPoint(new Vector2(ms.X, ms.Y)))
                {
                    _isChecked = !_isChecked;
                }
            }
        }
    }
}
